package org.example.test.test;

/**
 * 二分查找
 */
public class Search {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 7, 9, 12, 14, 25};
        int search = search(arr, 13);
        System.out.println(search);
    }

    public static int search(int[] nums, int target) {
        return search(nums, target, 0, nums.length - 1);
    }

    public static int search(int[] nums, int target, int left, int right) {
        if (left > right) {
            return -1;
        }
        int mid = (left + right) / 2;
        if (nums[mid] == target) {
            return mid;
        } else if (nums[mid] > target) {
            return search(nums, target, left, mid - 1);
        } else {
            return search(nums, target, mid + 1, right);
        }
    }


}
